package com.tfjybj.itoo.exam.provider.service.impl;

import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.ClassEntity;
import com.tfjybj.itoo.exam.model.ClassModel;
import com.tfjybj.itoo.exam.provider.dao.ClassDao;
import com.tfjybj.itoo.exam.provider.service.ClassService;
import com.tfjybj.itoo.exam.provider.service.StudentService;
import com.tfjybj.itoo.exam.template.ScreenClassStu;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.tfjybj.itoo.exam.provider.until.YearAcquisitionUtil.yearAcquisition;

/**
 * ClassService接口实现类
 * ${base}表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 10:35:56
 */
@Service("classService")
public class ClassServiceImpl extends BaseServicePlusImpl<ClassDao, ClassEntity> implements ClassService {

    //region 模板生成

    @Resource
    private ClassDao classDao;
    @Resource
    private StudentService studentService;


    @Override
    public PageInfo<ClassEntity> queryByLikeName(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(classDao.queryLikeName(name));
    }

    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据professionId查找所有行政班信息
     *
     * @param professionId 专业id
     * @return classEntityList
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public List<ClassEntity> findByProfessionId(String professionId) {
        return classDao.findByProfessionId(professionId);
    }

    @Override
    public List<ScreenClassStu> querySchoolClass(String screenClassName, String majorId, String startTime, String stuNameOrStuCode, String endTime) {
        List<ScreenClassStu> screenClassStuList = new ArrayList<>();
        // 根据专业id和在校生最小届入学年份和在校生最大届入学年份 查询
        List<ClassModel> classModelList = classDao.querySchoolClass(majorId, yearAcquisition().get("minSession"), yearAcquisition().get("maxSession"), screenClassName);

        // 遍历班级集合查询班级下面对应的考生
        classModelList.forEach(classModel -> {
            ScreenClassStu screenEnter = new ScreenClassStu();
            screenEnter.setKey(classModel.getId());
            screenEnter.setDisableCheckbox(false);
            // 查询本班下面的未在考试的学生
            List<ScreenClassStu> studentModelList = studentService.queryNoExamination(classModel.getId(), startTime, stuNameOrStuCode, endTime);
            screenEnter.setTitle(classModel.getName() + "-->班级人数:" + studentModelList.size() + "人");
            screenEnter.setChildren(studentModelList);
            screenClassStuList.add(screenEnter);
        });

        return screenClassStuList;
    }


}
